using System;
using System.Diagnostics;
using SmartSite.Frame.Properties;
using static SmartSite.Frame.FrameTypeEnum;

namespace SmartSite.Frame
{

    /// <summary>
    /// 身份验证回应帧
    /// </summary>
    public class IdentityAckFrame : WithMonitorDeviceSnFrame
    {
        readonly FrameTypeEnum _Key = IdentityAckFrameType;
        public override FrameTypeEnum Key { get { return _Key; } }

        /// <summary>
        /// 返回验证结果 ，1字节
        /// </summary>
        readonly IValueField<ValidationCodeEnum> ValidationCodeField;
        public ValidationCodeEnum ValidationCode
        {
            get => ValidationCodeField.Value;
            set => ValidationCodeField.Value = value;
        }

        /// <summary>
        /// 通讯号. 3 Bytes. 由服务器分配
        /// </summary>
        readonly IValueField<ulong> CommunicationIdField;
        public long CommunicationId
        {
            get => (long)CommunicationIdField.Value;
            set => CommunicationIdField.Value = (ulong)value;
        }

        /// <summary>
        /// 时间. 4 Bytes
        /// </summary>
        readonly IValueField<DateTime> ServerDateTimeField;
        public DateTime ServerDateTime
        {
            get => ServerDateTimeField.Value;
            set => ServerDateTimeField.Value = value;
        }

        public IdentityAckFrame()
        : this(Array.Empty<byte>()) => InitFieldValue();

        protected IdentityAckFrame(byte[] buffer)
        : base(buffer)
        {
            ValidationCodeField = new ValidationCodeField(MonitorDeviceSnField);
            CommunicationIdField = new ULongField(ValidationCodeField, 24);
            ServerDateTimeField = new DateTimeField(CommunicationIdField);
            CRCField = new UShortField(ServerDateTimeField);
        }

        new void InitFieldValue()
        {
            base.InitFieldValue();
            FrameType = _Key;
            InfoLength = EvalInfoLength();
            Debug.Assert(InfoLength == 8);
        }
    }
}